﻿package com.toncen.samepms.system.domain;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.common.AbstractDomain;
import com.toncen.samepms.system.dao.ConfigurationDao;
import com.sky.common.clone.CloneUtils;
import com.sky.common.string.StringUtils;
import com.sky.dataimport.DynamicCellDataCell;
import com.sky.dataimport.DynamicCellDataRow;
import com.sky.dataimport.DynamicRowDataCell;
import com.sky.dataimport.DynamicRowDataRow;
import com.sky.dataimport.StaticDataCell;
import com.sky.dataimport.StaticDataRow;
import com.sky.dataimport.datatype.IntegerDataType;

/**
 * 描述：个性化设置表SYSTEM_SKIN_CONFIGURATION的domain文件
 * 
 * @author zq
 */
@DynamicRowDataRow(title = "动态行个性化设置", sheetIndex = 1, startRowIndex = 2, startCellIndex = 1, inserted = false)
@DynamicCellDataRow(title = "动态列个性化设置", sheetIndex = 1, startRowIndex = 2, startCellIndex = 2, inserted = false)
@StaticDataRow(title = "静态行列个性化设置", sheetIndex = 1)
public class SkinConfiguration extends AbstractDomain {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1338272150611L;

	/**
	 * 用户
	 */
	@StaticDataCell(rowIndex = 1, cellIndex = 1, title = "用户")
	@DynamicRowDataCell(cellIndex = 1, title = "用户", require = false)
	@DynamicCellDataCell(rowIndex = 1, title = "用户", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private long userId;

	private String theme;

	/**
	 * 主题皮肤
	 */
	@StaticDataCell(rowIndex = 2, cellIndex = 2, title = "主题皮肤")
	@DynamicRowDataCell(cellIndex = 2, title = "主题皮肤", require = false)
	@DynamicCellDataCell(rowIndex = 2, title = "主题皮肤", require = false)
	private String themeSkin;

	/**
	 * 列表皮肤
	 */
	@StaticDataCell(rowIndex = 3, cellIndex = 3, title = "列表皮肤")
	@DynamicRowDataCell(cellIndex = 3, title = "列表皮肤", require = false)
	@DynamicCellDataCell(rowIndex = 3, title = "列表皮肤", require = false)
	private String gridSkin;

	/**
	 * 页面皮肤
	 */
	@StaticDataCell(rowIndex = 4, cellIndex = 4, title = "页面皮肤")
	@DynamicRowDataCell(cellIndex = 4, title = "页面皮肤", require = false)
	@DynamicCellDataCell(rowIndex = 4, title = "页面皮肤", require = false)
	private String pageSkin;

	/**
	 * 弹出窗口皮肤
	 */
	@StaticDataCell(rowIndex = 5, cellIndex = 5, title = "弹出窗口皮肤")
	@DynamicRowDataCell(cellIndex = 5, title = "弹出窗口皮肤", require = false)
	@DynamicCellDataCell(rowIndex = 5, title = "弹出窗口皮肤", require = false)
	private String asyncboxSkin;

	/**
	 * 描述
	 */
	@StaticDataCell(rowIndex = 6, cellIndex = 6, title = "描述")
	@DynamicRowDataCell(cellIndex = 6, title = "描述", require = false)
	@DynamicCellDataCell(rowIndex = 6, title = "描述", require = false)
	private String discription;

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public SkinConfiguration() {
		super();
	}

	/**
	 * 获取默认皮肤配置
	 * 
	 * @return
	 * @throws Exception
	 */
	public static SkinConfiguration getDefaultSkinConfiguration() throws Exception {
		SkinConfiguration defaultSkinCon = new SkinConfiguration();
		ConfigurationDao cd = new ConfigurationDao();
		try {
			defaultSkinCon.setTheme(cd.getByName("system_theme").getValue());
		} catch (NullPointerException excepton) {
			throw new Exception("未设置默认主题[system_theme]，请与系统管理员联系！");
		}
		try {
			defaultSkinCon.setThemeSkin(cd.getByName("system_theme_skin").getValue());
		} catch (NullPointerException excepton) {
			throw new Exception("未设置默认主题的皮肤[system_theme_skin]，请与系统管理员联系！");
		}
		try {
			defaultSkinCon.setAsyncboxSkin(cd.getByName("system_asyncbox_skin").getValue());
		} catch (NullPointerException excepton) {
			throw new Exception("未设置默认弹出窗口的皮肤[system_asyncbox_skin]，请与系统管理员联系！");
		}
		try {
			defaultSkinCon.setGridSkin(cd.getByName("system_grid_skin").getValue());
		} catch (NullPointerException excepton) {
			throw new Exception("未设置默认列表的皮肤[system_grid_skin]，请与系统管理员联系！");
		}
		try {
			defaultSkinCon.setPageSkin(cd.getByName("system_page_skin").getValue());
		} catch (NullPointerException excepton) {
			throw new Exception("未设置默认页面的皮肤[system_page_skin]，请与系统管理员联系！");
		}
		return (SkinConfiguration) CloneUtils.deepClone(defaultSkinCon);
	}

	/**
	 * 
	 * 从cookies取个性化配置
	 * 
	 * @author May 29, 2012 7:13:02 PM ZQ
	 * @param request
	 * @param skinCon
	 * @return
	 * @throws Exception
	 * 
	 */
	public static SkinConfiguration getSkinConfigurationFromCookies(HttpServletRequest request) throws Exception {
		SkinConfiguration skinCon = getDefaultSkinConfiguration();
		Cookie[] cookies = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : request.getCookies()) {
				if ("system_theme_skin".equalsIgnoreCase(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
					// 系统皮肤
					skinCon.setThemeSkin(cookie.getValue());
				}
				if ("system_grid_skin".equalsIgnoreCase(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
					// 列表皮肤
					skinCon.setGridSkin(cookie.getValue());
				}
				if ("system_page_skin".equalsIgnoreCase(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
					// 页面皮肤
					skinCon.setPageSkin(cookie.getValue());
				}
				if ("system_asyncbox_skin".equalsIgnoreCase(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
					// 弹出窗皮肤
					skinCon.setAsyncboxSkin(cookie.getValue());
				}
				if ("system_theme".equalsIgnoreCase(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
					// 弹出窗皮肤
					skinCon.setTheme(cookie.getValue());
				}
			}
		}
		return skinCon;
	}

	/**
	 * @return the userId
	 */
	public long getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(long userId) {
		this.userId = userId;
	}

	/**
	 * @return the themeSkin
	 */
	public String getThemeSkin() {
		return themeSkin;
	}

	/**
	 * @param themeSkin
	 *            the themeSkin to set
	 */
	public void setThemeSkin(String themeSkin) {
		this.themeSkin = themeSkin;
	}

	/**
	 * @return the gridSkin
	 */
	public String getGridSkin() {
		return gridSkin;
	}

	/**
	 * @param gridSkin
	 *            the gridSkin to set
	 */
	public void setGridSkin(String gridSkin) {
		this.gridSkin = gridSkin;
	}

	/**
	 * @return the pageSkin
	 */
	public String getPageSkin() {
		return pageSkin;
	}

	/**
	 * @param pageSkin
	 *            the pageSkin to set
	 */
	public void setPageSkin(String pageSkin) {
		this.pageSkin = pageSkin;
	}

	/**
	 * @return the asyncboxSkin
	 */
	public String getAsyncboxSkin() {
		return asyncboxSkin;
	}

	/**
	 * @param asyncboxSkin
	 *            the asyncboxSkin to set
	 */
	public void setAsyncboxSkin(String asyncboxSkin) {
		this.asyncboxSkin = asyncboxSkin;
	}

	/**
	 * @return the discription
	 */
	public String getDiscription() {
		return discription;
	}

	/**
	 * @param discription
	 *            the discription to set
	 */
	public void setDiscription(String discription) {
		this.discription = discription;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

}
